//
//  WBStatusViewModel.swift
//  czWB
//
//  Created by Qing on 2019/1/19.
//  Copyright © 2019年 成哥哥. All rights reserved.
//

import UIKit

@objcMembers class WBStatusViewModel: CustomStringConvertible {
    
    /// 单条微博模型
    var status: WBStatuses
    
    /// 昵称旁边的VIP标识icon， 会员等级1-6级
    var vipIconView: UIImage?
    
    /// 认证类型， -1：没有认证， 0：认证用户， 2，3，5：企业认证， 220：达人
    var verifiedTypeIcon: UIImage?
    
    /// 单条微博配图
//    var userPicUrls: [[String: Any]]?
    
    /// 转发数描述
    var reposts_countString: String?
    /// 评论数描述
    var comments_countString: String?
    /// 点赞数描述
    var attitudes_countString: String?
    
    
    /// 构造函数
    ///
    /// - Parameter status: 传入一个WBStatuses类型的值，赋值给成员status
    init(model: WBStatuses) {
        
        self.status = model
        
//        设置vip等级图标
        if let mbrank = status.user?.mbrank {
            if mbrank > 0 && mbrank < 7 {
//                commen_imgae_menber_vip1
                self.vipIconView = UIImage(named: "commen_imgae_menber_vip\(mbrank)")
            }
        }
        
//        设置认证图标
        switch status.user?.verified_type {
        case 0:
            self.verifiedTypeIcon = UIImage(named: "avater_vip")
        case 2, 3, 5:
            self.verifiedTypeIcon = UIImage(named: "avater_greassroot")
        case 220:
            verifiedTypeIcon = UIImage(named: "avater_enterprise_vip")
        default:
            break
        }
        
//        计算分享数/评论/点赞数
        attitudes_countString = countString(count: status.attitudes_count, defultString: "点赞")
        comments_countString = countString(count: status.comments_count, defultString: "评论")
        reposts_countString = countString(count: status.reposts_count, defultString: "转发")
        
        
//        guard let urlString = status.user?.profile_image_url else {
//            return
//        }
//
//        avatarIconView?.cz_setImage(urlSting: urlString, placeholderImage: nil)
        
    }
    
    /// 给定义一个数字，返回对应的结果
    ///
    /// - Parameters:
    ///   - count: 数字
    ///   - defultString: 默认字符串，分享/评论/点赞
    /// - Returns: 描述结果
    private func countString(count: Int, defultString: String) -> String {

//        额外需求
//        如果==0，显示默认标题
//        如果数量<10000，则显示实际数量
//        如果数量超过10000，显示x.xx万
        if count == 0 {
            
            return defultString
        }
        
        if count < 10000 {
            return count.description
        }
        return String(format: "%.02f 万", Double(count) / 10000)
        
    }
    
    var description: String {
        
        return status.description
    }

}
